<?php
namespace app\admin\service;

use app\Exception;
use app\admin\model\AdminUser;
use think\Validate;

class AdminUserManger
{
    /**
     * 通过用户名查找用户
     */
    function findByUsername($username)
    {
        return AdminUser::getQuery()->where('username','=',$username)->find();
    }

    /**
     * 查找用户
     */
    function query($pageParam)
    {
        return AdminUser::getQuery()->paginate($pageParam);
    }



    /**
     * 通过id查找用户
     */
    function find($id)
    {
        $query = AdminUser::getQuery();
        return $query->where($query->getPk(),'=',$id)->find();
    }

    /**
     * 添加管理员
     */
    function insert($data)
    {
        $data = array_only($data,['username','email','role_id','enable','password','name']);
        $va =new  Validate;
        $va->rule([
            'username'=>'require|r2:',
            'enable'=>'integer',
            'role_id'=>'require|integer|r1:',
        ])->extend('r1',function($role_id){
            $role = app(RoleManger::class)->find($role_id);
            return !!$role?true:'不存在的角色';
        })->extend('r2',function($username){
            $user = $this->findByUsername($username);
            return !$user?true:"用户名已存在";
        });
        if(isset($data['password']) && $data['password'] != ''){
            $data['password'] = $this->createPassword($data['password'] );
        }
        if(!$va->check($data)){
            throw new Exception($va->getError());
        }
        return AdminUser::create($data);
    }

    /**
     * 删除
     * @param int $id
     */
    function delete($id)
    {
        AdminUser::getQuery()->where(AdminUser::getQuery()->getPk(),'=',$id)->delete();
    }
    /**
     * 修改
     * @param int $id
     */
    function edit($id,$data)
    {
        $data = array_only($data,['username','email','role_id','enable','password','name']);
        $va =new  Validate;
        $va->rule([
            'username'=>'r2:',
            'enable'=>'integer',
            'role_id'=>'integer|r1:',
        ])->extend('r1',function($role_id,$param,$data){
            if(isset($data['role_id'])){
                $role = app(RoleManger::class)->find($role_id);
                return !!$role?true:'不存在的角色';
            }
            return true;
        })->extend('r2',function($username,$param,$data) use($id){
            if(isset($data['username'])){
                $user = $this->findByUsername($username);
                if(!$user || $user['id'] == $id){
                    return true;
                }
                return "用户名已存在";
            }
            return true;
        });
        if(isset($data['password']) && $data['password'] != ''){
            $data['password'] = $this->createPassword($data['password'] );
        }
        if(!$va->check($data)){
            throw new Exception($va->getError());
        }
        AdminUser::getQuery()->where(AdminUser::getQuery()->getPk(),'=',$id)->update($data);
    }


    function checkPassword($password,$hashPassword)
    {
        return $this->createPassword($password) == $hashPassword;
    }

    function createPassword($password)
    {

        $hashPassword = md5(md5($password).config('app.safe_salt'));
        return $hashPassword;
    }
}